cc.Class({
    extends: cc.Component,

    properties: {
        air: cc.Node,

        fireboomPrefab: cc.Prefab,

        phLabel: cc.Label,

        passFireLabel: cc.Label,

        levelLabel: cc.Label,

        welcome: cc.Node,

        scoreLabel: cc.Label,

        //飞机的血量
        ph: 0,

        //产生火球的间隔时间
        fireTimeDurastion: 0,

        //计时器
        _timer: 0,

        //等级
        _level: 0,

        //提升等级需要躲避火球的个数
        levelUpCount: 0,

        //已经躲过火球的个数
        _passFireBoomCount: 0,

        //游戏暂停
        _pause: false,

    },

    onLoad: function () {
        cc.director.getCollisionManager().enabled = true;
        this._timer = 0;
        this._level = 1;
        this._passFireBoomCount = 0;
        this._pause = true;
        Global.score = 0;
        this.air.getComponent('air').game = this;
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    },

    update: function (dt) {
        if (this._pause) {
            return;
        }
        this._timer++;
        if (this._timer % this.fireTimeDurastion == 0) {
            this.newFireBoom();
        }
    },

    onTouchStart(event) {
        console.log('event ' + event.getLocationX() + ' x ' + this.node.width/2);
        if (event.getLocationX() < this.node.width/2) {
            this.air.getComponent('air').turnLeft();
        }else if (event.getLocationX() > this.node.width/2) {
            this.air.getComponent('air').turnRight();
        }
    },

    //继续游戏
    startGame: function () {
        this._pause = false;
        this.welcome.destroy();
    },

    //躲避一个火球
    passFireBoom: function () {
        this._passFireBoomCount++;
        this.passFireLabel.string = 'PASSFIRE : ' + this._passFireBoomCount;
        Global.score += this._level * 10;
        this.scoreLabel.string = 'SCORE : ' + Global.score;
        if (this._passFireBoomCount % this.levelUpCount == 0) {
            this.levelUp();
        }
    },

    //提升等级
    levelUp: function () {
        this._level = this._passFireBoomCount / this.levelUpCount + 1;
        this.levelLabel.string = 'LEVEL : ' + this._level;
        if (this.fireTimeDurastion > 5) {
            this.fireTimeDurastion -= 5;
        }
    },

    //生成火球
    newFireBoom: function () {
        this._timer = 0;
        var fireb = cc.instantiate(this.fireboomPrefab);
        this.node.addChild(fireb);
        fireb.setPosition(this.getRandomPosition());
        fireb.getComponent('fireboom').game = this;
    },

    //获取火球的随机位置
    getRandomPosition: function () {
        var maxX = this.node.width / 2;
        var ranX = cc.randomMinus1To1() * maxX;
        var ranY = this.node.height / 2;
        return cc.p(ranX, ranY);
    },

    //飞机掉血
    dropBlood: function () {
        this.ph--;
        this.phLabel.string = 'PH : ' + this.ph.toString();
        var score = Global.score - this._level * 20;
        if (score < 0) {
            score = 0;
        }
        Global.score = score;
        if (this.ph <= 0) {
            this.gameOver();
            return;
        }
    },

    //游戏结束
    gameOver: function () {
        if (Global.score > Global.heightScore) {
            Global.heightScore = Global.score;
        }
        console.log('heightScore ' + Global.heightScore + ' score ' + Global.score);
        cc.director.loadScene('gameOver');
    },

});
